// components/publish-lost-form/publish-lost-form.js
import {getDate, verifyInput, verifyPhone} from "../../utils/Util";
import Notify from "@vant/weapp/notify/notify";
import {getStorageSync} from "../../utils/Storage";
import {requestSubscribeMessage} from "../../utils/Wx";
import Static from "../../utils/Static";
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        formData: { //表单数据
            type: Object,
            value: {
                type: "lost",
                name: "",
                intro: "",
                phone: "",
                images: [],
                time: getDate().split(" ")[0],
                region: "",
                regionDetail: ""
            }
        },
        buttonText: {
            type: String,
            value: "发布"
        },
        loadingText: {
            type: String,
            value: "发布中..."
        },
        loading: {
            type: Boolean,
            value: false
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        _fileSavePath: "", //上传文件路径
        _timePop: {
            show: false,
            value: new Date().getTime(),
            maxDate: new Date().getTime(),
        },
        _regionShow: false,
        _regionLoading: false, //区域加载
        _loading: false,
        _checkAuthorize: false //是否开启用户授权检测
    },

    ready: function () {
        let userInfo = getStorageSync("userInfo") || {};
        let phone =  this.data.formData.phone || userInfo.phone;
        this.setData({
            _checkAuthorize: true,
            _fileSavePath: "record/" + (userInfo.openId ? userInfo.openId + "/" : ""),
            "formData.phone": phone || ""
        });
    },

    /*
     * 数据监听
     * */
    observers: {
        loading: function (value) {
            this.setData({_loading: value})
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /*
        * 图片接收函数
        * */
        onUploadFile: function (e) {
            this.data.formData.images = e.detail.result.map(function (item) {
                return item.id
            })
        },

        /*
        * 授权监听函数
        * */
        onAuthorizeBtn: function (e) {
            this.setData({"formData.phone": e.detail.result})
        },

        /*
        * input输入内容
        * */
        onChange: function (e) {
            let name = e.currentTarget.dataset.name;
            this.setData({["formData." + name]: e.detail})
        },

        /*
        * 日期相关事件
        * */
        onTimeClick: function () { //点击cell
            this.setData({
                "_timePop.show": true
            })
        },
        onTimeConfirm: function (e) { //日期组件确定按钮
            this.setData({
                "formData.time": getDate(e.detail).split(" ")[0],
                "_timePop.value": e.detail,
                "_timePop.show": false
            })
        },
        onTimeClose: function () { //关闭弹框
            this.setData({
                "_timePop.show": false
            })
        },

        /*
        * 定位相关事件
        * */
        onRegionClick: function () {
            if (!this.data._regionLoading) { //防重复
                this.setData({_regionShow: true})
            }
        },
        onSelectRegion: function (e) {
            let name = e.detail.name;
            let result = e.detail.result;
            if (name === "loading") {
                this.setData({_regionLoading: true})
            } else if (name === "complete") {
                this.setData({_regionLoading: false})
            } else {
                this.setData({
                    _regionShow: false,
                    _regionLoading: false,
                    "formData.region": result.region,
                    "formData.regionDetail": result.regionDetail,
                })
            }
        },

        /*
        * 点击验证发布
        * */
        onSubmitBtn: async function () {
            const that = this;
            let formData = that.data.formData;
            let name = formData.name,
                intro = formData.intro,
                phone = formData.phone;
            let loading = that.data._loading;
            if (!verifyInput(name)) {
                Notify({
                    context: that,
                    selector: "#van-notify_lost",
                    message: "丢失物件名称不能为空",
                });
                return false
            } else if (!verifyInput(intro)) {
                Notify({
                    context: that,
                    selector: "#van-notify_lost",
                    message: "丢失物件概述不能为空"
                });
                return false
            } else if (!verifyInput(phone)) {
                Notify({
                    context: that,
                    selector: "#van-notify_lost",
                    message: "联系方式不能为空"
                });
                return false
            } else if (!verifyPhone(phone)) {
                Notify({
                    context: that,
                    selector: "#van-notify_lost",
                    message: "联系方式不正确"
                });
                return false
            } else if (loading) {
                return false
            } else {
                let res = await requestSubscribeMessage([Static.templateId.shtz, Static.templateId.lytz])
                for (let i in res) {
                    if (res[i] === "accept") {
                        formData[i] = formData[i] ? formData[i] + 1 : 1
                    }
                }
                that.setData({_loading: true});
                that.triggerEvent("addLostRecord", formData, {});
            }
        }
    }
})

